#!/usr/bin/env python
# -*- coding:utf-8 -*- #
from popcan.biz.accessors import AlbumAccessor
from popcan.helper.requesthelper import JsonResult, getParam as _p, respError
from popcan.helper.viewhelper import require_login, require_designer
from popcan.helper.validhelper import validDigit

@require_login
@require_designer
def getAlbumInfo(request):
    customer = request.affix['customer']
    albumId = _p(request,'albumId','')
    if not albumId:
        return respError('albumId_empty','how would I know which album u want')
    if not validDigit(albumId):
        return respError('albumId_invalid','invalid albumId')
    albumId = long(albumId)
    album = AlbumAccessor().getAlbum(albumId)
    if not album or album.custId <> customer.custId:
        return respError('album_empty','u have no such album')
    result = JsonResult()
    result.retdata['album'] = album
    return result.response()

@require_login
@require_designer
def getAlbumList(request):
    customer = request.affix['customer']
    albums = AlbumAccessor().getAlbumList(customer.custId)
    result = JsonResult()
    result.retdata['albums'] = albums
    return result.response()

@require_login
@require_designer
def getAlbumDesc(request):
    customer = request.affix['customer']
    albumId = _p(request,'albumId','')
    if not albumId:
        return respError('albumId_empty','how would I know which album desc u want')
    if not validDigit(albumId):
        return respError('albumId_invalid','invalid albumId')
    albumId = long(albumId)
    album = AlbumAccessor().getAlbum(albumId)
    if not album or album.custId <> customer.custId:
        return respError('album empty','you have no such album')
    desc = AlbumAccessor().getDesc(albumId)
    if not desc:desc = ''
    result = JsonResult()
    result.retdata['desc'] = desc
    return result.response()

@require_login
@require_designer
def create(request):
    name = _p(request,'name').strip()
    desc = _p(request,'desc').strip()
    if not name:
        return respError('name_empty','album must have a name')
    customer = request.affix['customer']
    album = AlbumAccessor().createAlbum(customer.custId, name, desc)
    result = JsonResult()
    result.retdata['albumId'] = album.albumId
    return result.response()

@require_login
@require_designer
def remove(request):
    albumId = _p(request,'albumId','')
    if not albumId:
        return respError('albumId_empty','how would I know which album to remove')
    if not validDigit(albumId):
        return respError('albumId_invalid','invalid albumId')
    albumId = long(albumId)
    customer = request.affix['customer']
    if not AlbumAccessor().removeAlbum(customer.custId, albumId):
        return respError('albumId_notexists','you have no such album')
    return JsonResult().response()

@require_login
@require_designer
def modifyDesc(request):
    customer = request.affix['customer']
    albumId = _p(request,'albumId','')
    if not albumId:
        return respError('albumId_empty','how would I know which album to change')
    if not validDigit(albumId):
        return respError('albumId_invalid','invalid albumId')
    albumId = long(albumId)
    desc = _p(request,'desc','').strip()
    if not desc:
        return respError('desc_empty','desc must supported')
    if not AlbumAccessor().modifyDesc(customer.custId, albumId, desc):
        return respError('album empty','you have no such album')
    return JsonResult().response()

@require_login
@require_designer
def modifyName(request):
    customer = request.affix['customer']
    albumId = _p(request,'albumId','')
    if not albumId:
        return respError('albumId_empty','how would I know which album to change')
    if not validDigit(albumId):
        return respError('albumId_invalid','invalid albumId')
    albumId = long(albumId)
    name = _p(request,'name').strip()
    if not name:
        return respError('name_empty','album must have a name')
    if not AlbumAccessor().modifyName(customer.custId, albumId, name):
        return respError('album empty','you have no such album')
    return JsonResult().response()
    